/**
 * 
 */
package com.ekingstar.framework.action;

import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;
import net.sf.json.util.PropertyFilter;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.domain.SystemParameter;
import com.ekingstar.framework.domain.SystemParameter.ParameterType;
import com.ekingstar.framework.service.ISystemParameterService;

/**
 * 
 *
 */
@Action(name="list",namespace="/systemParameter")
public class ListSystemParameterAction extends ActionSupport{

	private SystemParameter systemParameter;
	
	public SystemParameter getSystemParameter() {
		return systemParameter;
	}

	private String node;
	private Map types;
	private Map parents;
	public Map getTypes() {
		return types;
	}

	public void setTypes(Map types) {
		this.types = types;
	}

	public Map getParents() {
		return parents;
	}

	public void setParents(Map parents) {
		this.parents = parents;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public void setSystemParameter(SystemParameter systemParameter) {
		this.systemParameter = systemParameter;
	}

	private ISystemParameterService systemParameterService;
	
	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}
	//显示参数
	public String getChildren(){
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out = response.getWriter();
			response.setContentType("application/json; charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			if (systemParameter == null) {systemParameter = new SystemParameter();}
			systemParameter.setParentId(node);
			List sps = systemParameterService.querySystemParameters(systemParameter);
			//json
		    JsonConfig jc = new JsonConfig();
		    //
		    jc.registerJsonBeanProcessor(SystemParameter.class, new JsonBeanProcessor(){
				public JSONObject processBean(Object bean, JsonConfig jsonConfig) {
					// TODO Auto-generated method stub
					SystemParameter sp = (SystemParameter)bean;
					JSONObject val = new JSONObject();
					val.element("id",sp.getId());
					val.element("name",sp.getName());
					if (sp.getType() != null){
						SystemParameter.ParameterType type = SystemParameter.ParameterType.valueOf(sp.getType());
						if (type != null){
							val.element("type",type.toString());
						}
					}
					//
					val.element("detail",sp.getDetail());
					return val;
				}
		    });
		   
			JSONArray funcs = JSONArray.fromObject(sps,jc);
			out.print(funcs.toString());
			
			
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	public String deleteSP(){
		if (systemParameter != null && systemParameter.getId() != null){
			systemParameterService.deleteSystemParameter(systemParameter.getId());
		}
		ParameterType[] pts = ParameterType.values();
		if (types == null) types = new LinkedHashMap();
		types.put(null, "请选择");
		for (int i =0 ;i < pts.length;i++){
			types.put(pts[i].name(), pts[i].toString());
		}
		return "list";
	}
	public String execute(){
		ParameterType[] pts = ParameterType.values();
		if (types == null) types = new LinkedHashMap();
		types.put(null, "请选择");
		for (int i =0 ;i < pts.length;i++){
			types.put(pts[i].name(), pts[i].toString());
		}
		//
		
		List res = systemParameterService.querySystemParameters(systemParameter);
		return "list";
	}
	
	
	
	
}
